package day3.HomeWork;

/**
 * ClassName：TestTicked
 *
 * @Author：小鱼
 * @Create：2025-08-20 9:54
 */
public class Ticked implements Runnable{
    //总票数
    private static int tickets = 50;
    //使用同步代码块、
    //创建一个锁对象
    Object mutex = new Object();
    @Override
    public void run() {

        while (true) {
            synchronized (mutex) {
                if (tickets <= 0) {
                    break;
                }
                String name = Thread.currentThread().getName();
                System.out.println("售票口：" + name + "正在售票" + tickets);
                tickets--;
            }
            try {
                //每个窗口出票后 随机休眠时间范围在[100,200]ms。
                int sleepTime = 100 + (int)(Math.random() * 101);
                Thread.sleep(sleepTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}
